<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function pan(hand, groupSize) {
      hand.sort((a, b) => a - b);
      const map = hand.reduce((acc, n) => acc.set(n, (acc.get(n) || 0) + 1), new Map());
      for (const n of hand) {
        if (!map.get(n)) continue;
        for (let i = 0; i < groupSize; i++) {
          if (!map.get(n + i)) return false;
          else map.set(n + i, map.get(n + i) - 1);
        }
      }
      return true;
    };
    console.log(pan([1, 2, 3, 6, 2, 3, 4, 7, 8], 3));
  </script>
</body>

</html>